<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="user"
              :model="user"
              :rules="rules"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="user.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input v-model="user.newPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetail, saveUserDetailById } from '@/api/user'
import UserInfo from './components/UserInfo'
import JobInfo from './components/JobInfo'
export default {
  name: 'EmployeeDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      user: {
        username: '',
        newPassword: '' // 提供新的密码绑定, 回显旧密码无意义。v-model绑定的字段就是对象被覆盖一样会有（有则改无则加）
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空' }
        ],
        newPassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度6-16位' }
        ]
      }
    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  created() {
    this.getUserInfo() // 获取个人信息回显 不用vuex是因为这里是所有的员工, 而不仅仅是登录的
  },
  methods: {
    async getUserInfo() {
      this.user = await getUserDetail(this.id)
    },
    // 保存用户登录账户信息：用户名和密码
    async saveUser() {
      // 表单校验
      try {
        await this.$refs.user.validate()
      } catch {
        return
      }
      await saveUserDetailById({
        id: this.id,
        username: this.user.username,
        password: this.user.newPassword
      })
      this.$message.success('修改成功')
    }
  }
}
</script>

<style>

</style>
